﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MyMoviesBrowserLibrary.Properties;
using System.IO;

namespace MyMoviesBrowserLibrary
{
    public class MyMoviesDatabase
    {
        const string CONFIG_LOCATION = @"MyMoviesBrowser";
        public string ErrorMessage { get; set; }
        public bool CheckDataBaseConnection()
        {
            SqlConnection conn = new SqlConnection(string.Format(MyMoviesBrowserLibrary.Properties.Settings.Default.MyMoviesDatabaseConnection, Settings.Default.MyMoviesServerName));
            SqlCommand cmd = new SqlCommand("select count(*) from sys.databases where name = 'My Movies'", conn);

            using (conn)
            {
                try
                {
                    int count = 0;
                    conn.Open();
                    count = (int)cmd.ExecuteScalar();
                }
                catch (SqlException ex)
                {
                    ErrorMessage = ex.Message;
                    return false;
                }
            }
            return true;
        }

        public bool CheckAndCreateDatabase()
        {
            string location = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), CONFIG_LOCATION);
            SqlConnection conn = new SqlConnection(string.Format(MyMoviesBrowserLibrary.Properties.Settings.Default.MyMoviesDatabaseConnection, Settings.Default.MyMoviesServerName));
            SqlConnection conn2 = new SqlConnection(string.Format(Settings.Default.MyMoviesBrwoserConnectionString2, Settings.Default.MyMoviesServerName));
            SqlConnection conn3 = new SqlConnection(string.Format(Settings.Default.MyMoviesBrwoserConnectionString2, Settings.Default.MyMoviesServerName));
            try
            {
                if (!Directory.Exists(location))
                    Directory.CreateDirectory(location);

                SqlCommand cmd = new SqlCommand("select count(*) from sys.databases where name = 'MyMoviesBrowser'", conn);
                SqlCommand cmd2 = new SqlCommand(string.Format(Resources.MyMoviesBrowser, location), conn);
                SqlCommand cmd3 = new SqlCommand(Resources.MyMoviesBrowser2, conn2);
                SqlCommand cmd4 = new SqlCommand(Resources.TvManager, conn3);
                using (conn)
                {
                    conn.Open();
                    int count = 0;
                    count = (int)cmd.ExecuteScalar();

                    if (count == 0)
                    {
                        cmd2.ExecuteNonQuery();
                        using (conn2)
                        {
                            conn2.Open();
                            cmd3.ExecuteNonQuery();
                        }
                    }
                    using (conn3)
                    {
                        if (conn3.State != System.Data.ConnectionState.Open)
                            conn3.Open();
                        cmd4.ExecuteNonQuery();
                    }

                }
            }
            catch (SqlException ex)
            {
                ErrorMessage = ex.Message;
                return false;
            }
            return true;

        }
    }
}
